package com.ydgk.dao;

import com.ydgk.bean.Order;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.lang.Nullable;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrderDao extends BaseDao<Order> {
    @Override
    public int save(Order order) {
        return 0;
    }

    @Override
    public int updateById(Order order) {
        return this.template.update("update y_order set orderform=? where id=?",order.getOrderform(),order.getId());
    }

    @Override
    public int deleteById(int id) {
        return template.update("delete from y_order where id=?",id);
    }

    @Override
    public List<Order> findAll() {
        return null;
    }

    @Override
    public Order findById(int id) {
        return null;
    }

    @Override
    public List<Order> paging(int pageNumber) {
        return null;
    }

    public List<Order> adminSearch(String oderform, Integer pageNumber) {
        StringBuilder sql = new StringBuilder("SELECT o.id as oid,u.name,u.phone,u.address,o.money,o.ono,o.pay,o.orderform,o.time,o.userId FROM y_user u,y_order o where o.userId=u.id ");
        List params = new ArrayList();
        if (oderform != null && !oderform.equals("")) {
            sql.append(" and o.orderform = ?");
            params.add(oderform);
        }
        sql.append(" order by oid asc limit ?,? ");
        params.add((pageNumber - 1) * pageCount);
        params.add(pageCount);
        return this.template.query(sql.toString(), new ResultSetExtractor<List<Order>>() {
            @Nullable
            @Override
            public List<Order> extractData(ResultSet rs) throws SQLException, DataAccessException {
                List<Order> orderList = new ArrayList<Order>();
                while (rs.next()) {
                    Order order = new Order();
                    order.setId(rs.getInt("oid"));
                    order.getUser().setName(rs.getString("name"));
                    order.getUser().setPhone(rs.getString("phone"));
                    order.getUser().setAddress(rs.getString("address"));
                    order.setMoney(rs.getInt("money"));
                    order.setOno(rs.getString("ono"));
                    order.setPay(rs.getString("pay"));
                    order.setOrderform(rs.getString("orderform"));
                    order.setTime(rs.getTimestamp("time"));
                    order.setUserId(rs.getInt("userId"));
                    orderList.add(order);
                }
                return orderList;
            }
        },params.toArray());
    }
    public int adminSearchMaxPage(String oderform) {
        StringBuilder sql = new StringBuilder("SELECT count(*) FROM y_user u,y_order o where o.userId=u.id");
        List params = new ArrayList();
        if (oderform != null && !oderform.equals("")) {
            sql.append(" and o.orderform = ?");
            params.add(oderform);
        }
        long t = this.template.queryForObject(sql.toString(),Long.class,params.toArray());

        int total = (int)t;//将long类型转换成int类型
        return total % pageCount == 0 ? total / pageCount : total / pageCount + 1 ;
    }
}
